from django import forms
from ask.models import Question, Answer
from tagging.fields import TagField
from django.conf import settings

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification
else:
    notification = None

if "activity_stream" in settings.INSTALLED_APPS:
    from activity_stream.models import ActivityTypes
    from activity_stream.models import create_activity_item
else:
    create_activity_item = None

class AddQuestionForm(forms.ModelForm):
    user = None
    def __init__(self,*args,**kwargs):
        self.user = kwargs.get('user',None)
        kwargs['user'] = self.user
        del kwargs['user']
        super(AddQuestionForm,self).__init__(*args,**kwargs)
        
    title = forms.CharField(max_length=100)
    text = forms.CharField(widget=forms.Textarea(),max_length=1000)
    tags = TagField()
    
    class Meta:
        model = Question    
        exclude = ['user','to','markup','views']
        
    def save(self):
        if self.instance and self.instance.id:
            i = self.instance
            user = i.user
            try:
                profile = Profile.objects.get(user=user)
                i.markup = profile.markup
            except:
                pass
#                i.markup = 'mrk'
            i.title = self.cleaned_data.get('title','')
            i.text = self.cleaned_data.get('text','')
            i.status = self.cleaned_data.get('status',True)                 
            i.tags = self.cleaned_data.get('tags','')
            i.save()
            if "activity_stream" in settings.INSTALLED_APPS:
                if i.status == settings.CONTENT_TR['public'] and 'status' in self.changed_data:
                    create_activity_item("ask_question", self.user, i, safetylevel=1, custom_date=None)
                else:
                    create_activity_item("edit_question", self.user, i, safetylevel=3, custom_date=None)
                    
            return i
        else:
            d = {
                 'user': self.user,
                 'title': self.cleaned_data.get('title',''),
                 'text': self.cleaned_data.get('text',''),
                 'status': self.cleaned_data.get('status',True),
                 'tags': self.cleaned_data.get('tags','')             
            }
            try:
                user = self.user
                profile = Profile.objects.get(user=user)
                d['markup'] = profile.markup
            except:
#                traceback.print_exc()
                pass
            q = Question(**d)
            q.save()
            if q.status == settings.CONTENT_TR['public']:
                if "activity_stream" in settings.INSTALLED_APPS:
                    create_activity_item("ask_question", self.user, q, safetylevel=1, custom_date=None)
            return q

class AddAnswerForm(forms.ModelForm):
 
    user = None
    question = None
    
    def __init__(self,*args,**kwargs):
        self.user = kwargs.get('user',None)
        kwargs['user'] = self.user
        del kwargs['user']
        self.question = kwargs.get('question',None)
        kwargs['question'] = self.question
        del kwargs['question']
        super(AddAnswerForm,self).__init__(*args,**kwargs)
     
    class Meta:
        model = Answer
        exclude = ['user', 'question', 'markup', 'status']
        
    def save(self):
        if (self.instance.id):
            self.instance.text = self.cleaned_data.get('text','')
            self.instance.save()
            if self.instance.status == settings.CONTENT_TR['public']:
                if "activity_stream" in settings.INSTALLED_APPS:
                    create_activity_item("edit_answer", self.user, self.instance, safetylevel=3, custom_date=None)
        else:
            d = {
                 'user' : self.user,
                 'question': self.question,
                 'text': self.cleaned_data.get('text','')
            }
            a = Answer(**d)
            a.save()
            if notification:
                notification.send([self.question.user], "answer_on_your_question", {"answer": a})
            if a.status == settings.CONTENT_TR['public']:
                if "activity_stream" in settings.INSTALLED_APPS:
                    create_activity_item("answer_question", self.user, a, safetylevel=1, custom_date=None)
            